<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/1 0001
 * Time: 下午 4:45
 */
class RecommendModel
{
    private $recommendKeyPrefix  	=   'RDP/';

    public function __construct(){
        $this->diaryListCacheModel      =   new DiaryListCacheModel();
    }

    /**
     * 保存热门点滴
     * @param $topicId
     * @param $bodyId
     * @return bool|int|resource
     */
    public function saveHotDiary($topicId, $bodyId) {
        $diaryInfo = $this->getTopicHotDiaryByBodyId($topicId, $bodyId);
        if(!empty($diaryInfo)) {
            //软删除旧数据
            $this->removeTopicHotDiary($topicId, $bodyId);
        }
        return $this->addHotDiary($topicId, $bodyId);
    }

    /**
     * @param $topicId
     * @param $bodyId
     * @return bool|int|resource
     */
    public function addHotDiary ($topicId, $bodyId) {
        if(empty($topicId) || empty($bodyId)) {
            AC::$errorNo = 'RDM001';    //参数为空
            return false;
        }
        $data = array(
            'type'          =>  AC::RECOMMEND_TYPE_DIARY,
            'int1'          =>  $topicId,
            'type_id'       =>  $bodyId,
            'category'      =>  AC::RECOMMEND_CATEGORY_HOT_DIARY,
            'create_at'    =>  AC::SERVER_TIME,
            'update_at'    =>  AC::SERVER_TIME
        );

        $key    =   $this->recommendKeyPrefix. 0;
        $result =   Storage::addData($key, $data);
        if (empty($result))
        {
            AC::$errorNo    =   'RDM002';
            return  FALSE;
        }

        return  $result;
    }

    /**
     * 将现有的热门全部置为过期
     * @return bool|int|resource
     */
    public function removeTopicHotDiaryList() {
        $data = array(
            'status' => 1,
            'update_at'    =>  AC::SERVER_TIME
        );
        $where = array(
            'status'        => 0,
            'type'          =>  AC::RECOMMEND_TYPE_DIARY,
            'category'      =>  AC::RECOMMEND_CATEGORY_HOT_DIARY,
        );
        $key    =   $this->recommendKeyPrefix. 0;
        $result = Storage::setConditionData($key, $data, $where);
        if (empty($result))
        {
            AC::$errorNo    =   'RDM002';
            return  FALSE;
        }

        return  $result;
    }


    /**
     * 将改点滴已存在的热门设为过期
     * @param $topicId
     * @param $bodyId
     * @return bool|int|resource
     */
    public function removeTopicHotDiary($topicId, $bodyId) {
        $data = array(
            'status' => 1,
            'update_at'    =>  AC::SERVER_TIME
        );
        $where = array(
            'status'        => 0,
            'int1'          => $topicId,
            'type_id'       => $bodyId,
            'type'          =>  AC::RECOMMEND_TYPE_DIARY,
            'category'      =>  AC::RECOMMEND_CATEGORY_HOT_DIARY,
        );
        $key    =   $this->recommendKeyPrefix. 0;
        $result = Storage::setConditionData($key, $data, $where);
        if (empty($result))
        {
            AC::$errorNo    =   'RDM002';
            return  FALSE;
        }

        return  $result;
    }

    /**
     * @param $topicId
     * @return array|bool
     */
    public function getTopicHotDiary($topicId) {
        $where = array(
            'status'        => 0,
            'int1'          => $topicId,
            'type'          =>  AC::RECOMMEND_TYPE_DIARY,
            'category'      =>  AC::RECOMMEND_CATEGORY_HOT_DIARY,
        );
        $key    =   $this->recommendKeyPrefix. 0;
        $result = Storage::getConditionManyData($key, $where);
        return  $result;
    }

    /**
     * @param $topicId
     * @param $bodyId
     * @return array|bool
     */
    public function getTopicHotDiaryByBodyId($topicId, $bodyId) {
        $where = array(
            'status'        => 0,
            'int1'          => $topicId,
            'type'          => AC::RECOMMEND_TYPE_DIARY,
            'type_id'       => $bodyId,
            'category'      => AC::RECOMMEND_CATEGORY_HOT_DIARY,
        );
        $key    =   $this->recommendKeyPrefix. 0;
        $result = Storage::getConditionManyData($key, $where);
        return  $result;
    }

}